package com.lihao.sam.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lihao.sam.dao.CollegeSettingDao;
import com.lihao.sam.dto.CollegeDto;
import com.lihao.sam.dto.CompanyEmpCountDto;
import com.lihao.sam.entity.CollegeSetting;
import com.lihao.sam.entity.CompanyEmp;
import com.lihao.sam.service.intf.CollegeSettingService;
import com.lihao.sam.vo.PageVo;
import com.lihao.sam.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @ClassName CollegeSettingServiceImpl
 * @Description TODO
 * @Author LiHao
 * @Date 2022/11/23 13:31
 * @Version 1.0
 */
@Service
public class CollegeSettingServiceImpl implements CollegeSettingService {
    @Resource
    private CollegeSettingDao dao;

    @Override
    public R add(CollegeSetting collegeSetting) {
        if (dao.add(collegeSetting) > 0) {
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public R update(CollegeSetting collegeSetting) {
        if (dao.update(collegeSetting) > 0) {
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public R delete(int id) {
        if (dao.delete(id) > 0) {
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public PageVo queryPage(int page, int limit, String collegeName, String deptName, String major) {
        PageHelper.startPage(page, limit);
        PageInfo<CollegeSetting> pageInfo = new PageInfo<>(dao.selectByWhere(collegeName, deptName, major));
        return new PageVo(0, "OK", pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R uploadExcel(MultipartFile file) throws IOException {
        if (file != null) {
            InputStream inputStream = file.getInputStream();
            List<CollegeSetting> list = EasyExcel.read(inputStream, CollegeSetting.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                }
            }).sheet().doReadSync();
            if (dao.addBatch(list) > 0) {
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public void download(HttpServletResponse response) throws IOException {
        //设置下载的文件名
        String filename = "college_" + UUID.randomUUID() + ".xlsx";
        //设置响应消息头，实现文件下载
        response.setHeader("content-disposition", "attachment;filename=" + filename);
        //写出要下载的内容
        List<CollegeSetting> list = dao.selectAll();
        EasyExcel.write(response.getOutputStream(),CollegeSetting.class).sheet().doWrite(list);
    }

    @Override
    public R deleteBatch(int[] array) {
        if (dao.delBatch(array) > 0) {
            return R.ok();
        }
        return R.fail();
    }

    /** 统计报表 */
    @Override
    public R queryCollegeMajorCount() {
        //1.查询
        List<CollegeDto> list = dao.selectCollegeMajorCount();
        //2.组装结果数据
        Map<String, ArrayList> map = new HashMap<>();
        map.put("deptName", new ArrayList());
        map.put("num", new ArrayList());
        for (CollegeDto dto : list) {
            map.get("deptName").add(dto.getDeptName());
            map.get("num").add(dto.getNum());
        }
        return R.ok(map);
    }

}
